package com.gcloud.configuracion;

//import com.gcloud.*;  
//import com.gcloud.librerias.*;
//import com.gcloud.login.LoginValidator;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import org.json.JSONArray;
//import org.json.JSONException;
//import org.json.JSONObject;
//import android.app.ListActivity;
//import android.app.ProgressDialog;
//import android.content.Intent;
//import android.os.AsyncTask;
//import android.util.Log;
//import android.widget.ListAdapter;
//import android.widget.SimpleAdapter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import com.gcloud.persistance.SQLiteHandler;
import com.utils.Constants;
import com.utils.GCloudUtil;
import com.utils.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import com.example.gcode.*; 

public class Cuentas extends Activity{
	
	Button button1//, button2, button3
	;
	Spinner accountSpinner;
	String service="";
	
//	private static String KEY_SUCCESS = "success";
//	private static String KEY_ERROR = "error";
//	private static String KEY_ERROR_MSG = "error_msg";
	private final static int DROPBOX=0;
	private final static int GOOGLEDRIVE=1;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cuentas_layout);
		 addSpinnerListener();
		button1 = (Button) findViewById(R.id.button1);
		//button2 = (Button) findViewById(R.id.button2);
		//button3 = (Button) findViewById(R.id.button3);
		
		button1.setOnClickListener(new View.OnClickListener() {			
			
			@Override
			public void onClick(View view){
				/**Going to change to account management**/
				EditText user=(EditText)findViewById(R.id.emailRegisterNewAccount);
//				EditText key=(EditText)findViewById(R.id.passRegisterNewAccount);	
				String userAccount=user.getText().toString();
//				String passwordAccount=key.getText().toString();
				if(!GCloudUtil.isEmpty(userAccount)){
					showToast(GCloudUtil.Message(Message.FIELD_EMPTY, new String[]{"User"}));
					return;
				}
//					else if(!GCloudUtil.isEmpty(passwordAccount)){
//					showToast(GCloudUtil.Message(Message.FIELD_EMPTY, new String[]{"Key"}));
//					return;
//				}											
//				SQLiteHandler sqliteHandler=new SQLiteHandler(getApplicationContext(), Constants.GCLOUD, null, Constants.DB_VERSION);
//				sqliteHandler.insertAccount(service,  userAccount, passwordAccount);				
//				showToast("Cuenta agregada :"+user.getText().toString()+" Servicio: "+service);
				/**END**/
				if(service.equals(Constants.GOOGLE)){
					Intent intentnew = new Intent(Cuentas.this,
							GoogleDriveSyncronized.class);
					startActivity(intentnew);
				}else{
					Intent intentdrop = new Intent(Cuentas.this,
							DropBoxSyncronized.class);
					startActivity(intentdrop);
				}
				
				
//				String usuario = "giancarlo.greina@gmail.com";
//				String clave = "12345";
//				String servicio = "1";
//				String users ="1";
//				
//				UserFunctions userFunction = new UserFunctions();
//				userFunction.agregarCuenta(usuario, clave, users, servicio);
			}
		});
		
//		button2.setOnClickListener(new View.OnClickListener() {			
//			
//			@Override
//			public void onClick(View view){
//				String usuario = "1";
//				
//				UserFunctions userFunction = new UserFunctions();
//				userFunction.obtenerCuentas(usuario);
//			}
//		});
		
//		button3.setOnClickListener(new View.OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				String id_cuenta = "1";
//				
//				UserFunctions userFunction = new UserFunctions();
//				userFunction.eliminarCuenta(id_cuenta);
//				
//				
//			}
//		});
	}
	
	private void addSpinnerListener(){
		accountSpinner=(Spinner)findViewById(R.id.accountSpinner);
		accountSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> component, View view,
					int position, long arg3) {
				switch(position){
				case DROPBOX:
					service=Constants.DROPBOX;
					break;
				case GOOGLEDRIVE:
					service=Constants.GOOGLE;
					break;
				}								
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
	}
	
	public void showToast(final String toast) {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				Toast.makeText(getApplicationContext(), toast,
						Toast.LENGTH_LONG).show();
			}
		});
	}
	
}
